package com.shire.shire_interface.controller.rule;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.rule.WxRuleGroup;
import com.shire.shire_interface.model.rule.WxRuleGroupExample;
import com.shire.shire_interface.service.rule.WxRuleGroupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

//@Api(tags = "群管理 自动踢人")
@RestController
@RequestMapping("/rule")
public class WxRuleGroupController extends BaseController {

    @Resource
    private WxRuleGroupService wxRuleGroupService;

//    @ApiOperation(value = "插入&&更新 csid, wxaccounts, rulejson")
    @ResponseBody
    @PostMapping("/insert")
    public Response insert(@RequestBody WxRuleGroup wxRuleGroup, HttpSession session) {
        Response response = new Response();
        try {
            Integer csId = wxRuleGroup.getCsid();
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            WxRuleGroupExample wxRuleGroupExample = new WxRuleGroupExample();
            wxRuleGroupExample.createCriteria().andCsidEqualTo(csId);
            List<WxRuleGroup> wxRuleGroups = wxRuleGroupService.selectByExample(wxRuleGroupExample);
            if (wxRuleGroups.size() > 0) {
                wxRuleGroupService.updateByExampleSelective(wxRuleGroup, wxRuleGroupExample);
            } else {
                wxRuleGroupService.insertSelective(wxRuleGroup);
                response.setData(wxRuleGroup);
            }
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "查询 csId")
    @ResponseBody
    @PostMapping("/get")
    public Response get(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            WxRuleGroupExample wxRuleGroupExample = new WxRuleGroupExample();
            wxRuleGroupExample.createCriteria().andCsidEqualTo(csId);
            List<WxRuleGroup> wxRuleGroups = wxRuleGroupService.selectByExample(wxRuleGroupExample);
            if (wxRuleGroups.size() > 0) {
                WxRuleGroup wxRuleGroup = wxRuleGroups.get(0);
                response.setData(wxRuleGroup);
            }
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
